package com.zabra.zl.ui.godeal;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.DetailsDealBean;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.myview.CircularProgressView;
import com.zabra.zl.ui.buy.BuyActivity;
import com.zabra.zl.ui.detalsdeal.DetailsDealActivity;
import com.zabra.zl.ui.godeal.contract.GoDealContract;
import com.zabra.zl.ui.godeal.presenter.GoDealPresenter;
import com.zabra.zl.ui.godeal.tab.tab_haved.Adp;
import com.zabra.zl.ui.nullpage.NullPersonActivity;
import com.zabra.zl.ui.profit.ProfitActivity;
import com.zabra.zl.ui.recall.RecallActivity;
import com.zabra.zl.ui.sale.SaleActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoDealActivity extends BaseMvpActivity<GoDealPresenter> implements GoDealContract.View {


    @BindView(R.id.bt_buy)
    LinearLayout btBuy;
    @BindView(R.id.bt_sale)
    LinearLayout btSale;
    @BindView(R.id.bt_recall)
    LinearLayout btRecall;
    @BindView(R.id.bt_deposit)
    LinearLayout btDeposit;
    @BindView(R.id.bt_dealdetals)
    LinearLayout btDealdetals;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_orderid)
    TextView tvOrderid;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_ketou)
    TextView tvKetou;
    @BindView(R.id.tv_ccsz)
    TextView tvCcsz;
    @BindView(R.id.tv_ccbl)
    TextView tvCcbl;
    @BindView(R.id.tv_fudong)
    TextView tvFudong;
    @BindView(R.id.tv_yujing)
    TextView tvYujing;
    @BindView(R.id.tv_pingcang)
    TextView tvPingcang;
    @BindView(R.id.progerss)
    CircularProgressView progerss;
    @BindView(R.id.iv_upordown)
    ImageView ivUpordown;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.rl_nodata)
    RelativeLayout rlNodata;
    private int orderId;
    private Adp adp;
    private Dialog dialog;
    private Bundle bundle = new Bundle();

    @Override
    public int getLayoutId() {
        return R.layout.activity_go_deal;
    }

    @Override
    public void initView() {
        orderId = getIntent().getExtras().getInt("orderid");
        mPresenter = new GoDealPresenter();
        mPresenter.attachView(this);
        keyList.add("bpOrderId");
        valueList.add(orderId + "");
        mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));

    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(DetailsDealBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            tvMoney.setText(bean.getPositionVal());
            tvOrderid.setText("操盘方案号：" + bean.getOrderNo());
            tvKetou.setText(bean.getUseBalance());
            tvCcsz.setText(bean.getTotMarketVal());
            tvCcbl.setText(bean.getPositionRatio());
            tvFudong.setText(bean.getTotEarnAmt());
            tvYujing.setText(bean.getWarningAmt());
            tvPingcang.setText(bean.getCloseAmt());
            if (bean.getTotEarnAmt().contains("-")) {
                tvFudong.setTextColor(Color.parseColor("#06b96a"));
                ivUpordown.setImageResource(R.mipmap.icon_down1);
            } else {
                tvFudong.setTextColor(Color.parseColor("#ff3025"));
                ivUpordown.setImageResource(R.mipmap.up);
            }

            double v1 = Double.parseDouble(bean.getTotMarketVal());
            double all = Double.parseDouble(bean.getPositionVal());
            double v2 = v1 / all;
            int pro = (int) (v2 * 100);
            progerss.setProgress(pro);

            //持仓数组
            List<DetailsDealBean.PositionsBean> list = bean.getPositions();
            if (list.size() == 0) {
                llTitle.setVisibility(View.GONE);
                rlNodata.setVisibility(View.VISIBLE);
            } else {
                llTitle.setVisibility(View.VISIBLE);
                rlNodata.setVisibility(View.GONE);
                recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
                adp = new Adp(R.layout.item_haved, list);
                recycler.setAdapter(adp);
                adp.openLoadAnimation();
            }

        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else if (bean.getRespCode().equals(Config.NONAME)) {
            EventMessage message = new EventMessage(1, "Deal");
            EventBus.getDefault().postSticky(message);
            toClass1(this, NullPersonActivity.class);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }

    }


    @OnClick({R.id.back, R.id.bt_buy, R.id.bt_sale, R.id.bt_recall, R.id.bt_deposit, R.id.bt_dealdetals})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.bt_buy:
                //买入
                bundle.putInt("orderid", orderId);
                toClass(this, BuyActivity.class, bundle);
                break;
            case R.id.bt_sale:
                //卖出
                bundle.putInt("orderid", orderId);
                toClass(this, SaleActivity.class,bundle);
                break;
            case R.id.bt_recall:
                //撤单
                bundle.putInt("orderid", orderId);
                toClass(this, RecallActivity.class,bundle);
                break;
            case R.id.bt_deposit:
                //提盈
                bundle.putInt("orderid", orderId);
                toClass(this, ProfitActivity.class,bundle);
                break;
            case R.id.bt_dealdetals:
                //交易明细
                bundle.putInt("orderid", orderId);
                toClass1(this, DetailsDealActivity.class,bundle);
                break;
        }
    }

}
